package webshop.servlets;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import webshop.beans.admin.FUBean;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import database.entity.ShopImage;
import database.entity.ShopItem;
import database.entitymanager.ShopImageManager;
import database.entitymanager.ShopItemManager;

/**
 * Servlet implementation class ImageHandleServlet
 */
public class ImageHandleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ImageHandleServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Servlet#getServletConfig()
	 */
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] url = request.getRequestURI().split("/");

		if (url.length < 5) {
			response.sendError(404);
			return;
		}
		int shopItemId;
		int imageIndex;

		try {
			shopItemId = Integer.parseInt(url[4]);
			imageIndex = Integer.parseInt(url[5]);
			if (shopItemId < 0 || imageIndex < 0) {
				System.out.println("Ne postoje velicine < 0");
				response.sendError(404);
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
			response.sendError(404);
			return;
		}
		try {
			ShopItem si = ShopItemManager.findShopItem(shopItemId);
			List<ShopImage> sil = si.getShopImageList();
			if (sil.size() < imageIndex) {
				response.sendError(404);
				return;
			}

			ShopImage shopImage = sil.get(imageIndex - 1);
			File file = new File(FUBean.contextPath, shopImage.getShopimagerelativepath());
			if (!file.exists()) {
				System.out.println("File does not exist , deleting");
				ShopImageManager.destroy(shopImage.getShopimageid());
			}

			JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new FileInputStream(file));
			BufferedImage image = decoder.decodeAsBufferedImage();

			// Send back image
			ServletOutputStream sos = response.getOutputStream();
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
			encoder.encode(image);
		} catch (Exception e) {
			e.printStackTrace();
			response.sendError(404);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
